﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SangsTools;
using Battle;
using System.Threading.Tasks;
using UI;

namespace Player
{
    public class MyPlayer : SingletonScript<MyPlayer>,UI.IPopUpWindowEventHandler
    {
        /// <summary>
        /// 阵地
        /// </summary>
        public Fort.Fort MyFort;
        /// <summary>
        /// 背包
        /// </summary>
        public readonly Backpack MyBackpack;
        /// <summary>
        /// 科研
        /// </summary>
        public readonly Science MyScience;
        /// <summary>
        /// 商店
        /// </summary>
        public readonly Shop MyShop;

        public MyPlayer()
        {
            MyBackpack = new Backpack();

            MyScience = new Science();
            MyShop = new Shop();

            EnemyMgr.Ins.EventOnEnemyDeath += OnEnemyDeath;
        }

        public async Task InitAsync()
        {
            await MyScience.InitAsync();
            await MyShop.InitAsync();

            MyBackpack.GetItem("Gun", 3);
            MyBackpack.BombCount = 1;
        }

        public void Reset()
        {
            MyScience.Reset();
            MyShop.Reset();
            MyBackpack.Reset();
            MyFort.Reset();

            EnemyMgr.Ins.Reset();

            MyBackpack.GetItem("Gun", 3);
            MyBackpack.BombCount = 1;
        }

        private void OnEnemyDeath(ST_EnemyDeathInfo deathInfo)
        {
            MyBackpack.Gold += deathInfo.nGoldReward;
            MyScience.ScienceNum += deathInfo.nScienceReward;
        }

        public void UseBomb()
        {
            if (MyBackpack.BombCount > 0)
            {
                EffMgr.CreateExplodeEffAsync(Vector3.zero, new ST_RangeData() { dRadius = 5 }, Color.white);
                EnemyMgr.Ins.Reset();
                MyBackpack.BombCount--;
            }
        }

        public void Death()
        {
            Debug.Log("Player Death");

            LevelScriptMgr.StopLevel();
            UI.UIPopUpWindow.ShowPopUpWindow(new UI.STPopUpWindowData("游戏结束", "GG", new string[] { "确定" }), this);
        }

        public async void OnChooseOption(int optionIndex, STPopUpWindowData uiData)
        {
            Reset();

            await Task.Delay(1000);

            LevelScriptMgr.NextLevel();
        }
    }

}